package cn.edu.swu.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;

public abstract class BasicServlet extends HttpServlet {
    protected SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-mm-dd");
    protected ObjectMapper objectMapper = new ObjectMapper();

    protected String getRequestBody(HttpServletRequest request) throws IOException {
        try(Reader reader = request.getReader()) {
            BufferedReader bufferedReader = new BufferedReader(reader);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        }
    }

    protected void responseJson(HttpServletResponse response, Boolean success, Object object) throws IOException {
        response.setContentType("application/json; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try(Writer writer = response.getWriter()) {
            objectMapper.setDateFormat(dateFormatter);
            ObjectWriter objectWriter = objectMapper.writer(dateFormatter);
            String json = objectWriter.writeValueAsString(JsonResponseWrapper.wrap(success, object));
            writer.write(json);
            writer.flush();
        }
    }

    protected String wrapPage(String pageContent) {
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html>")
                .append("<html lang=\"en\">")
                .append("<head><meta charset=\"UTF-8\"><title>西大网上书店</title>")
                .append("<link rel=\"stylesheet\" href=\"/bookstore/css/bookstore.css\">")
                .append("</head>")
                .append("<body>")
                .append("<div style=\"width:99%;background-color:#336699;color:#ffffff\"><marquee><h2>欢迎访问西南大学网上书店</h2></marquee></div>")
                .append("<center>")
                .append("<div class='menu'>")
                .append("<a class='menu_item' href='http://localhost:9090/bookstore/index.html'>首页</a>")
                .append("<a class='menu_item' href='/bookstore/book/list'>显示图书</a>")
                .append("<a class='menu_item' href='/bookstore/add_book.html'>添加图书</a>")
                .append("<a class='menu_item menu_item_right' href='/bookstore/logout'>退出系统</a>")
                .append("<form action='/bookstore/book/search' method='get' class='menu_item'><br>" +
                        "<input type='text' name='key'>&nbsp;&nbsp;&nbsp;" +
                        "<input type='submit' value='查询'>" +
                        "</form>")
                .append("</div>")
                .append(pageContent)
                .append("</center>")
                .append("</body>")
                .append("</html>");
        return sb.toString();
    }
}
